package com.projectName.www.service;


import com.projectName.www.po.orders;
import com.projectName.www.servlet.Y_J;
import com.projectName.www.util.String_util;
import com.projectName.www.dao.Y_J_buycar_dao;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.util.Vector;

/**
     * @author 86132
     *
     */
    public class Y_J_buycar extends JInternalFrame {
        private JTable dishe;
        private JTable coupon;
        private  int d_selectedRow;
             private  orders r=new orders();//选中的菜单

        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Y_J_buycar frame = new Y_J_buycar();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        /**
         * Create the frame.
         */
        public Y_J_buycar() throws Exception {
            setClosable(true);
            setBounds(100, 100, 915, 560);

            JScrollPane scrollPane = new JScrollPane();

            JScrollPane scrollPane_1 = new JScrollPane();

            JLabel lblNewLabel = new JLabel("领取的优惠券");
            lblNewLabel.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 34));

            JButton orders = new JButton("选中下单");
            orders.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 17));

            JButton close = new JButton("选中清除");
            close.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 17));
            GroupLayout groupLayout = new GroupLayout(getContentPane());
            groupLayout.setHorizontalGroup(
                    groupLayout.createParallelGroup(Alignment.LEADING)
                            .addGroup(groupLayout.createSequentialGroup()
                                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                            .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 442, GroupLayout.PREFERRED_SIZE)
                                            .addGroup(groupLayout.createSequentialGroup()
                                                    .addGap(57)
                                                    .addComponent(orders, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
                                                    .addGap(83)
                                                    .addComponent(close, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
                                            .addGroup(groupLayout.createSequentialGroup()
                                                    .addGap(54)
                                                    .addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 399, GroupLayout.PREFERRED_SIZE)
                                                    .addContainerGap())
                                            .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
                                                    .addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                    .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 230, GroupLayout.PREFERRED_SIZE)
                                                    .addGap(98))))
            );
            groupLayout.setVerticalGroup(
                    groupLayout.createParallelGroup(Alignment.LEADING)
                            .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
                                    .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                                            .addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
                                                    .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 414, GroupLayout.PREFERRED_SIZE)
                                                    .addGap(30)
                                                    .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                                            .addComponent(close, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
                                                            .addComponent(orders, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)))
                                            .addGroup(groupLayout.createSequentialGroup()
                                                    .addGap(25)
                                                    .addComponent(lblNewLabel)
                                                    .addPreferredGap(ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                                                    .addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 427, GroupLayout.PREFERRED_SIZE)))
                                    .addContainerGap())
            );

            coupon = new JTable();
            coupon.setModel(new DefaultTableModel(
                    new Object[][] {
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                    },
                    new String[] {
                            "\u4F18\u60E0\u989D\u5EA6", "\u662F\u5426\u8FC7\u671F", "\u662F\u5426\u4F7F\u7528\u8FC7", "\u9002\u7528\u5E97\u5BB6"
                    }
            ));
            scrollPane_1.setViewportView(coupon);

            dishe = new JTable();
            dishe.setModel(new DefaultTableModel(
                    new Object[][] {
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                    },
                    new String[] {
                            "\u83DC\u54C1", "\u6570\u91CF", "\u5F85\u652F\u4ED8\u91D1\u989D", "\u5E97\u5BB6"
                    }
            ));
            dishe.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 20));
            scrollPane.setViewportView(dishe);
            getContentPane().setLayout(groupLayout);
            filldishe();
            fillcou();
            orders.addActionListener(e->
                    {
                        try {
                            getorder(e);
                        } catch (Exception ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                    );
            dishe.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    d_selectedRow=dishe.getSelectedRow();
                    r.setBuyers(OpenBuyer.username);
                    r.setBusiness((String) dishe.getValueAt(d_selectedRow,3));
                    r.setDishename((String) dishe.getValueAt(d_selectedRow,0));
                    r.setMoney(String_util.num((String) dishe.getValueAt(d_selectedRow,2)) );
                    r.setNum(String_util.num((String) dishe.getValueAt(d_selectedRow,1)));
                }
            });
            close.addActionListener(e->
                    {
                        try {
                            closecar(e);
                        } catch (Exception ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                    );
        }

    private void closecar(ActionEvent e) throws Exception {

        if(Y_J_buycar_dao.close(r))
            filldishe();
        JOptionPane.showMessageDialog(null,"清除成功");

    }

    private void getorder(ActionEvent e) throws Exception {


       if(Y_J_buycar_dao.makeorder(r))
       {
           filldishe();
           Y_J.upmoney();
           JOptionPane.showMessageDialog(null,"下单成功");
    }
       else JOptionPane.showMessageDialog(null,"下单失败：请检查账户或者数量");
        }
    //填充购物车表格
        public void filldishe() throws Exception {
            ResultSet filldishe = Y_J_buycar_dao.filldishe(OpenBuyer.username);
           DefaultTableModel model= (DefaultTableModel) dishe.getModel();
           model.setRowCount(0);
            while(filldishe.next())
            {
                Vector v=new Vector();
                v.add(filldishe.getString("dishename"));
                v.add(filldishe.getString("num"));
                v.add(filldishe.getString("money"));
                v.add(filldishe.getString("business"));
                model.addRow(v);
            }
        }
        //填充优惠券表格
        public void fillcou() throws Exception {
            DefaultTableModel model=(DefaultTableModel)coupon.getModel();
            model.setRowCount(0);
         ResultSet  fillcou =Y_J_buycar_dao.fillcoupon(OpenBuyer.username);
         while (fillcou.next()) {
             Vector V = new Vector();
             V.add(fillcou.getString("coupon"));

             if (Y_J_buycar_dao.isUse(fillcou.getString("finaltime")))
                 V.add("否");
             else V.add("是");
             if (String_util.num(fillcou.getString("usernum")) > 0)
                 V.add("否");
             else V.add("是");
             V.add(fillcou.getString("shopname"));
             model.addRow(V);
         }
        }
}
